const { Controller } = require('egg');
const validate = require('../rule/rule');

class HomeController extends Controller {

  /**
   *
   * @param req
   * @param res
   * @api { POST } /user/relationship/createUserAndRole 分配角色
   * @apiName createUserAndRole
   * @apiGroup relationship
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { Number } user_id 用户id
   * @apiParam { Number } role_id 角色id
   *
   * @apiHeader { String } Authorization 注册信息
   *
   */
  async createUserAndRole(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { user_id, role_id } = validate.createUserAndRoleValidate(body);
      const data = ctx.service.relationship.createUserAndRole(user_id, role_id);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @param req
   * @param res
   * @api { POST } /user/relationship/updateUserAndRole 更新角色
   * @apiName updateUserAndRole
   * @apiGroup relationship
   * @apiVersion 1.0.0
   *
   * @apiParam { Number } user_id 用户id
   * @apiParam { Number } role_id 角色id
   *
   * @apiHeader { String } Authorization 注册信息
   *
   */
  async updateUserAndRole(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { user_id, role_id } = validate.updateUserAndRoleValidate(body);
      const data = ctx.service.relationship.updateUserAndRole(user_id, role_id);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @param req
   * @param res
   * @api { POST } /user/relationship/deleteUserAndRole 删除角色
   * @apiName deleteUserAndRole
   * @apiGroup relationship
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } user_id 用户id
   *
   * @apiHeader { String } Authorization 注册信息
   *
   */
  async deleteUserAndRole(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { user_id } = validate.deleteUserAndRoleValidate(body);
      const data = ctx.service.relationship.deleteUserAndRole(user_id);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @param req
   * @param res
   * @api { GET } /user/relationship/getRoleAndPermission 获取权限
   * @apiName getRoleAndPermission
   * @apiGroup relationship
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery { Number } role_id 用户id
   *
   * @apiHeader { String } Authorization 注册信息
   *
   */
  async getRoleAndPermission(req, res) {
    const ctx = this.ctx;
    try {
      const { query } = req;
      const { role_id } = validate.getRoleAndPermissionValidate(query);
      const data = ctx.service.relationship.getRoleAndPermission(role_id);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @param req
   * @param res
   * @api { POST } /user/relationship/createRoleAndPermission 创建角色权限
   * @apiName createRoleAndPermission
   * @apiGroup relationship
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { Number } role_id 用户id
   * @apiParam { Object } permission_id 权限id
   *
   * @apiHeader { String } Authorization 注册信息
   *
   */
  async createRoleAndPermission(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { role_id, permission_id } = validate.createRoleAndPermissionValidate(body);
      const data = ctx.service.relationship.createRoleAndPermission(role_id, permission_id);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @param req
   * @param res
   * @api { POST } /user/relationship/updateRoleAndPermission 编辑角色权限
   * @apiName updateRoleAndPermission
   * @apiGroup relationship
   * @apiVersion 1.0.0
   *
   *
   * @apiRequest json
   * @apiParam { String } role_id 用户id
   * @apiParam { Array } permission_id 权限id
   *
   *
   * @apiHeader { String } Authorization 注册信息
   *
   */
  async updateRoleAndPermission(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { role_id, permission_id } = validate.updateRoleAndPermissionValidate(body);
      const data = ctx.service.relationship.updateRoleAndPermission(role_id, permission_id);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @param req
   * @param res
   * @api { POST } /user/relationship/deleteRoleAndPermission 删除角色权限
   * @apiName deleteRoleAndPermission
   * @apiGroup relationship
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } role_id 用户id
   * @apiParam { Array } permission_id 权限id
   *
   * @apiHeader { String } Authorization 注册信息
   *
   */
  async deleteRoleAndPermission(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { role_id, permission_id } = validate.deleteRoleAndPermissionValidate(body);
      const data = ctx.service.relationship.deleteRoleAndPermission(role_id, permission_id);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  async init() {
    try {
      const data = await this.ctx.service.relationship.init();
      this.ctx.status = 200;
      this.ctx.sendSuccess({ data });
      return;
    } catch (error) {
      this.ctx.status = 200;
      this.ctx.sendFailed({ msg: error.message });
      return;
    }
  }
}

module.exports = HomeController;
